package com.crazy.crazyproductservice.controller;

import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazymodel.model.dto.product.ProductAddRequest;
import com.crazy.crazymodel.model.dto.product.ProductGetRequest;
import com.crazy.crazymodel.model.dto.product.ProductNetWorthRequest;
import com.crazy.crazymodel.model.dto.product.ProductUpdateRequest;
import com.crazy.crazymodel.model.vo.ProductAddVO;
import com.crazy.crazymodel.model.vo.ProductGetVO;
import com.crazy.crazymodel.model.vo.ProductNetWorthVO;
import com.crazy.crazymodel.model.vo.ProductUpdateVO;
import com.crazy.crazyproductservice.service.NetWorthService;
import com.crazy.crazyproductservice.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.util.Date;

/**
 * ProductController
 *
 * @author 13259
 * @version 2024/05/07 19:13
 **/
@RestController
@RequestMapping("/product")
@Slf4j
@Api(tags= "产品管理模块")
public class ProductController {
    @Resource
    private ProductService productService;

    @Resource
    private NetWorthService netWorthService;

    /**
     * 获取产品列表
     * @param productGetRequest 接收管理员查询请求的数据对象
     * @param request HTTP请求对象，用于获取请求信息
     * @return productGetVO
     */
    @ApiOperation(value="获取产品列表")
    @PostMapping ("/get")
    public BaseResponse<ProductGetVO> productGet(@RequestBody ProductGetRequest productGetRequest, HttpServletRequest request) {
        log.info("ProductGetRequest:{}", productGetRequest);
        int productCurrent=productGetRequest.getCurrent();
        String productName= productGetRequest.getName();
        String productType=productGetRequest.getType();
        String productState=productGetRequest.getState();
        ProductGetVO productGetVO=productService.productGet(productCurrent,productName,productType,productState,request);
        return ResultUtils.success(productGetVO);
    }

    /**
     * 新增产品
     * @param productAddRequest 接收管理员查询请求的数据对象
     * @param request HTTP请求对象，用于获取请求信息
     * @return productAddVO
     */
    @ApiOperation(value="添加产品")
    @PostMapping("/add")
    public BaseResponse<ProductAddVO> productAdd(@RequestBody ProductAddRequest productAddRequest, HttpServletRequest request) {
        log.info("ProductAddRequest:{}", productAddRequest);

        String productCode=productAddRequest.getCode();
        String productName= productAddRequest.getName();
        String productType=productAddRequest.getType();
        int productRisk=productAddRequest.getRisk();
        Date finishTime=null;
        String time=productAddRequest.getFinishtime();
        // 将前端传来的字符串解析为 Instant 对象
        if(time!=null) {
            Instant instant = Instant.parse(time);
            // 将 Instant 对象转换为 Date 对象
            finishTime = Date.from(instant);
            System.out.println(finishTime);
        }

        ProductAddVO productAddVO=productService.productAdd(productCode,productName,productType,productRisk,finishTime,request);
        return ResultUtils.success(productAddVO);
    }

    /**
     * 修改产品信息
     * @param productUpdateRequest 接收管理员查询请求的数据对象
     * @param request HTTP请求对象，用于获取请求信息
     * @return productUpdateVO
     */
    @ApiOperation(value="编辑产品")
    @PostMapping("/update")
    public BaseResponse<ProductUpdateVO> productUpdate(@RequestBody ProductUpdateRequest productUpdateRequest, HttpServletRequest request) {
        log.info("ProductUpdateRequest:{}", productUpdateRequest);

        int productId=productUpdateRequest.getId();
        String productCode=productUpdateRequest.getCode();
        String productName= productUpdateRequest.getName();
        String productType=productUpdateRequest.getType();
        int productRisk=productUpdateRequest.getRisk();
        Date finishTime=null;
        String time=productUpdateRequest.getFinishtime();
        if(time!=null) {
            Instant instant = Instant.parse(time);
            // 将 Instant 对象转换为 Date 对象
            finishTime = Date.from(instant);
            System.out.println(finishTime);
        }

        ProductUpdateVO productUpdateVO=productService.productUpdate(productId,productCode,productName,productType,productRisk,finishTime,request);
        return ResultUtils.success(productUpdateVO);
    }

    /**
     * 获取产品净值走势
     * @param productNetWorthRequest 接收管理员查询请求的数据对象
     * @param request HTTP请求对象，用于获取请求信息
     * @return productNetWorthVO
     */
    @ApiOperation(value="获取产品所有净值")
    @PostMapping("/getValue")
    public BaseResponse<ProductNetWorthVO> productGetValue(@RequestBody ProductNetWorthRequest productNetWorthRequest, HttpServletRequest request) {
        log.info("ProductNetWorthRequest:{}", productNetWorthRequest);

        int productId=productNetWorthRequest.getId();

        ProductNetWorthVO productNetWorthVO=netWorthService.productGetValue(productId,request);
        return ResultUtils.success(productNetWorthVO);
    }
}
